﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
/*using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
*/using System.Configuration;
using System.Data.SqlClient;
using App.Modelo.Attributes;

namespace App.Controlador.Seguridad
{
    [Formulario("Backup")]
    public partial class BackupForm : Form
    {
        /*
        SqlConnectionStringBuilder connection;
        Server server;
        
        public string UserName { get; set; }
        public string Password { get; set; }


        public BackupForm()
        {

            InitializeComponent();
            openFileDialog1.InitialDirectory 
                = saveFileDialog1.InitialDirectory 
                = AppDomain.CurrentDomain.BaseDirectory;
        }

        [Comando("ver")]
        public void ver()
        {
            Show();
        }
        ServerConnection serverConnection;
        public ServerConnection ServerConnection
        {
            get
            {
                if (serverConnection == null)
                {
                    var form = new security.LoginForm();
                    string u = ""; string p = "";
                    form.Mensaje = "Ingrese los privilegios de administrador de SQL Server";
                    form.Text = "SQL Server Login";
                    form.GetCredenciales(u,p, out u, out p);

                    if (form.DialogResult == DialogResult.OK)
                    {
                        UserName = u;
                        Password = p;
                        var conn = new ServerConnection(Connection.DataSource, UserName, Password);
                        try
                        {
                            conn.Connect();

                            serverConnection = conn;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                    form.Dispose();

                } 

                return serverConnection;

            }
        }
        public Server Server
        {
            get
            {
                if (null == server && ServerConnection != null)
                    server = new Server(ServerConnection);
                return server;
            }
            set
            {
                server = value;
            }
        }
        
        public SqlConnectionStringBuilder Connection
        {
            get
            {
                if (connection == null) 
                    connection = new SqlConnectionStringBuilder(Registry.Instance.Controlador.Connection.ConnectionString);

                return connection;
            }
            set
            {
                connection = value;
            }
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (Server == null)
                return;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var bdi = new BackupDeviceItem(saveFileDialog1.FileName, DeviceType.File);

                var backup = new Backup { Database = Connection.InitialCatalog, Initialize = true };

                backup.Devices.Add(bdi);
                
                try
                {
                    backup.SqlBackup(Server);
                    MessageBox.Show("Backup generado\n" + saveFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al generar el backup\n" + ex.Message);
                }
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (Server == null)
                return;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var r = new Restore();
                r.Database = Connection.InitialCatalog;

                r.Devices.Add(new BackupDeviceItem(openFileDialog1.FileName, DeviceType.File));
                r.ReplaceDatabase = true;
                Server.KillAllProcesses(r.Database);
                r.Wait();
                try
                {
                    r.SqlRestore(Server);
                    MessageBox.Show("Backup restablecido\n" + openFileDialog1.FileName );
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error al restituir el backup\n" + ex.ToString());
                }
            }
        }
*/
    }
}
